package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.TravelSupplierDto;
import com.ktgj.customer.domain.vo.TravelSupplierVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelSupplier;
import com.ktgj.customer.service.ITravelSupplierService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 供应商Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/supplier")
public class TravelSupplierController extends BaseController
{
    @Autowired
    private ITravelSupplierService travelSupplierService;

    /**
     * 查询供应商列表
     */
    //@PreAuthorize(hasPermi = "customer:supplier:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelSupplierDto travelSupplierDto)
    {
        startPage();
        List<TravelSupplierDto> list = travelSupplierService.selectTravelSupplierList(travelSupplierDto);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    //@PreAuthorize(hasPermi = "customer:supplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelSupplierDto travelSupplierDto) throws IOException
    {
        List<TravelSupplierDto> list = travelSupplierService.selectTravelSupplierList(travelSupplierDto);
        ExcelUtil<TravelSupplierDto> util = new ExcelUtil<TravelSupplierDto>(TravelSupplierDto.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    //@PreAuthorize(hasPermi = "customer:supplier:query")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        return AjaxResult.success(travelSupplierService.selectTravelSupplierById(supplierId));
    }

    /**
     * 新增供应商
     */
    //@PreAuthorize(hasPermi = "customer:supplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelSupplierDto travelSupplierDto)
    {
        return toAjax(travelSupplierService.insertTravelSupplier(travelSupplierDto));
    }

    /**
     * 修改供应商
     */
    //@PreAuthorize(hasPermi = "customer:supplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelSupplierDto travelSupplierDto)
    {
        return toAjax(travelSupplierService.updateTravelSupplier(travelSupplierDto));
    }

    /**
     * 删除供应商
     */
    //@PreAuthorize(hasPermi = "customer:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        return toAjax(travelSupplierService.deleteTravelSupplierByIds(supplierIds));
    }

    /**
     * 供应商列表
     * */
    @PostMapping("/travelSupplierVoList")
    public TableDataInfo travelSupplierVoList(@RequestBody Map<String, Object> map) {
        startPage();
        String supplierName = null;
        if (map.get("supplierName") != null && !map.get("supplierName").equals("")){
            supplierName = map.get("supplierName").toString();
        }
        List<TravelSupplierVo> travelSupplierVoList = travelSupplierService.travelSupplierVoList(supplierName);
        return getDataTable(travelSupplierVoList);
    }

    /**
     * 新增供应商
     * */
    @PostMapping("/addTravelSupplier")
    public AjaxResult addTravelSupplier(HttpServletRequest request, @RequestBody TravelSupplier travelSupplier){
        int i;
        try {
            i = travelSupplierService.addTravelSupplier(request, travelSupplier);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 修改供应商
     * */
    @PostMapping("/updTravelSupplier")
    public AjaxResult updTravelSupplier(@RequestBody TravelSupplier travelSupplier){
        int i;
        try {
            i = travelSupplierService.updTravelSupplier(travelSupplier);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 团供应商列表
     * */
    @PostMapping("/groupSupplierList")
    public AjaxResult groupSupplierList(){
        List<TravelSupplier> travelSupplierList = travelSupplierService.groupSupplierList();
        return AjaxResult.success(travelSupplierList);
    }
}
